package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.api.ApplicationConstant;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.view.StatusBarCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by gaowf on 2017/2/21.
 */

public class FeedbackRecordActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener{
    private final static String TAG = "FeedbackRecordActivity";
    private SharedPreferences mSp;
    private SimpleAdapter mAdapter;
    private ArrayList<Map<String,String>> mDataList;
    private String[] mStringArray = null;
    private int[] mIdArray;
    private Map<String,?> mAllDataMap;
    private ListView mFeedbackRecordList;
    private ImageView mReturnIV;
    //private String getCommitDate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback_record_activity);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }
        mFeedbackRecordList = (ListView)findViewById(R.id.feedback_record_list);
        mFeedbackRecordList.setOnItemClickListener(this);
        mReturnIV = (ImageView)findViewById(R.id.feedback_record_activity_return);
        mReturnIV.setOnClickListener(this);
        initData();
        initAdapter();
        mFeedbackRecordList.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    private List<Map<String,String>> getData() {
        getFeedbackList();
        mSp = getSharedPreferences(ApplicationConstant.SP_NAME, MODE_PRIVATE);
        mSp.getString(ApplicationConstant.SP_FEEDBACK_TITLE, "");
        mSp.getString(ApplicationConstant.SP_FEEDBACK_CONTENT, "");
        //getCommitDate = mSp.getString(ApplicationConstant.SP_FEEDBACK_DATE, "");
        mDataList = new ArrayList<Map<String,String>>();
        mAllDataMap = mSp.getAll();
        for(Map.Entry<String,?> entry:mAllDataMap.entrySet()){
            Map<String,String> map = new HashMap<String, String>() ;
            map.put(mStringArray[0],(String) entry.getKey());//主题名称
            //map.put(mStringArray[1],getResources().getString(R.string.feedback_title_title).toString());
            //if (((String) entry.getValue()).length() > 0) {
            try {
                map.put(mStringArray[1], ((String) entry.getValue()).substring(0, 19));//时间
                map.put(mStringArray[2], ((String) entry.getValue()).substring(19));//内容
            }catch (ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
            }
            mDataList.add(map);
        }
        return mDataList;
    }

    private void initData(){
        mStringArray =  new String[]{
                //ApplicationConstant.SP_FEEDBACK_STATUS,
                ApplicationConstant.SP_FEEDBACK_TITLE,
                ApplicationConstant.SP_FEEDBACK_DATE,
                ApplicationConstant.SP_FEEDBACK_CONTENT};
        mIdArray = new int[]{
                //R.id.feedback_record_title,
                R.id.feedback_record_lv_title,
                R.id.feedback_record_lv_date,
                R.id.feedback_content_lv};
    }

    private void initAdapter(){
        mAdapter = new SimpleAdapter(this, getData(), R.layout.feedback_record_list,
                mStringArray,mIdArray);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.feedback_record_activity_return){
            finish();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d("binbo","onItemClick VIEW ="+ parent.getAdapter().getItem(position));

        Map<String,String> map = (Map<String, String>) parent.getAdapter().getItem(position);

        Intent intent = new Intent(FeedbackRecordActivity.this,FeedbackDetailsActivity.class);
        intent.putExtra(ApplicationConstant.SP_FEEDBACK_TITLE,map.get(mStringArray[0]));

        startActivity(intent);
    }

    private void getFeedbackList(){
        String account = CDApplication.getIntance().getUserInfo().getUseraccount();
        HttpUtil.getFeedbackInfo(new FeedbackGetCallback(),account,account);
    }

    private class FeedbackGetCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, "feedback");
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    ArrayList<Object> data = resultBean.getData();

                    Log.d(TAG, "code = " + code + " message = " + message +" data"+ data);
                    if (code.equals("0")) {
                        showMessage(getResources().getString(R.string.feedback_send_success));
                    } else if (code.equals("99")) {
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getResources().getString(R.string.feedback_send_fail));
            }
        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);

            Log.d(TAG, "onFailure = " + error);
            if (error.toString().contains("refused")) {

            }
        }
    }

    /**
     *
     * @param string
     * 需要显示的字串提示
     */

    private void showMessage(String string) {
        Toast toast = Toast.makeText(FeedbackRecordActivity.this, string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

}
